//
//  ViewController.swift
//  Accounting
//
//  Created by Qingke on 16/5/4.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var passwordText: UITextField!
    @IBOutlet weak var tipLabel: UILabel!
    
    var nameID: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        nameText.becomeFirstResponder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func loginButton(sender: UIButton) {
        nameText.resignFirstResponder()
        passwordText.resignFirstResponder()
        guard let name = nameText.text,
            let password = passwordText.text else {
                tipLabel.text = "请输入用户名和密码"
                return
        }
        if   password != NSUserDefaults.standardUserDefaults().valueForKey(name) as? String  {
            tipLabel.text = "用户名或密码错误"
            tipLabel.backgroundColor = UIColor.redColor()
            return
        }else {
            tipLabel.text = "登陆成功"
            tipLabel.backgroundColor = UIColor.greenColor()
            nameID = name
            
            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("TotalViewController")  as? TotalViewController {
                controller.nameID = nameID
                presentViewController(controller, animated: true, completion: nil)//括号相当于新建一个实例化对象
            }
            
        }
        
    }
}
